package com.hutao.manage.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

public class CopyFile {
	public static void copyFileUsingFileChannels(File source, String copy)
			throws IOException {
		File copyFile=new File(copy);
		FileChannel inputChannel = null;
		FileChannel outputChannel = null;
		try {
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(copyFile).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		} finally {
			inputChannel.close();
			outputChannel.close();
		}
	}
	
	public static String fileType(File file){
		String fileType=null;
		if(file!=null){
			String fileName=file.getName();//根据后缀判断
	        fileType=fileName.substring(fileName.lastIndexOf("."));
		}
		return fileType;		
	}
	
	public static boolean folderExists(String folder){
		Boolean flag=false;
		if(folder!=null){
			File file =new File(folder);  
			
			//如果文件夹不存在则创建    
			if  (!file .exists()  && !file .isDirectory())      
			{       
			    System.out.println("//不存在");  
			     
			    flag=file .mkdirs();
			} else   
			{  
			    flag=false;
			}  
		
		}
		return flag;
	}
	//判断文件是否存在
	public static boolean fileExists(String fileName){
		Boolean flag=true;
		File file=new File(fileName);    
		if(!file.exists())    
		{    
		   flag=false;
		}    
		return flag;
	}
	
	
	public static boolean folderRename(String folder){
		Boolean flag=false;
		if(folder!=null){
			File file =new File(folder);  
			
			//如果文件夹不存在则创建    
			if  (!file .exists()  && !file .isDirectory())      
			{       
			    System.out.println("//不存在");  
			   
			    flag=true;
			} else   
			{  

			    file.renameTo(new File(folder+System.currentTimeMillis()));  
			    flag=false;
			}  
		
		}
		return flag;
	}	
	
	public static ArrayList<String> getFiles(String path) {
	    ArrayList<String> files = new ArrayList<String>();
	    File file = new File(path);
	    File[] tempList = file.listFiles();

	    for (int i = 0; i < tempList.length; i++) {
	        if (tempList[i].isFile()) {
	              System.out.println("文     件：" + tempList[i]);
	            files.add(tempList[i].toString());
	        }
	        if (tempList[i].isDirectory()) {
	              System.out.println("文件夹：" + tempList[i]);
	        }
	    }
	    return files;
	}
	
	public static void main(String[] args) throws IOException {
		 getFiles("D:\\hospital\\123\\practiceLicense");
		//copyFileUsingFileChannels(new File("e:\\1.pdf"),"e:\\2.pdf");
	}
}
